package com.supermap.dataservice.cloud.suggest;

import com.alibaba.dubbo.config.annotation.Service;
import com.supermap.dataservice.util.JSONUtil;
import com.supermap.dataservice.util.JSONUtils;
import com.supermap.dubbo.api.cloud.suggest.Suggest;
import com.supermap.dubbo.api.cloud.suggest.SuggestService;
import org.apache.ibatis.annotations.Param;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @author aqua
 * @description
 * @date 2018年11月26 下午 2:55
 * @email 1301584033@qq.com
 */
@Service
public class SuggestServiceImpl implements SuggestService {

    @Resource
    private SuggestMapper suggestMapper;

    @Override
    public JSONUtil putSuggest(Suggest suggest) {
        try {
            int count=suggestMapper.putSuggest(suggest);
            int rolut=suggestMapper.getSuggestId();
            //查询出作物相关的农田
            if(suggest.getCrop()!= null && !"".equals(suggest.getCrop())) {
                List<Integer> farmids = suggestMapper.getFarmIdsBycrop(suggest.getAreacode(), suggest.getCrop() + "");
                if (farmids != null && farmids.size() > 0) {
                    for (Integer farmid : farmids) {
                        suggestMapper.insertSuggestFarm(rolut, farmid);
                    }
                }
            }
            //写入suggestfarm关系表
            if(count>0){
                return  new JSONUtil().setCountSum(rolut);
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new JSONUtil().setCode(100).setMsg("操作失败").setIsSuccess(false);
    }

    @Override
    public JSONUtil getPageSuggestList(String userId, String farmId, String expertUserId, String suggestId, Integer pageNum, Integer pageSize,String keyName,String expertID) {
        pageNum=(pageNum-1)*pageSize;
        try {
            return  new JSONUtil().setData(suggestMapper.getPageSuggestList(userId,farmId,expertUserId,suggestId,pageNum,pageSize,keyName,expertID)).setCountSum(suggestMapper.getPageSuggestListCount(userId,farmId,expertUserId,suggestId,keyName,expertID));
        }catch (Exception e){
            e.printStackTrace();
        }
      return  new JSONUtil().setCode(100).setMsg("操作失败").setIsSuccess(false);
    }

    @Override
    public JSONUtil putPraiseSuggestList(String userId, String suggestId) {
        try {
            int count=suggestMapper.putPraiseSuggestList(userId,suggestId);
            if(count>0){
                return  new JSONUtil();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new JSONUtil().setCode(100).setMsg("操作失败").setIsSuccess(false);
    }

    @Override
    public JSONUtil delPraiseSuggestList(String userId, String suggestId) {
        try {
            int count=suggestMapper.delPraiseSuggestList(userId,suggestId);
            if(count>0){
                return  new JSONUtil();
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return  new JSONUtil().setCode(100).setMsg("操作失败").setIsSuccess(false);
    }

    @Override
    public List<Integer> getUserIdByAreaCodes(String areaCodes) {
        return suggestMapper.getUserIdByAreaCodes(areaCodes);
    }

    @Override
    public List<Integer> getFarmIdByAreaCodes(String areaCodes, String cropId) {
        return suggestMapper.getFarmIdByAreaCodes(areaCodes,cropId);
    }

    @Override
    public List<Map<String, Object>> getSuggestTypeList() {
        return suggestMapper.getSuggestTypeList();
    }

    @Override
    public List<Map<String, Object>> getinterUrlByUserId(String userId) {
        return suggestMapper.getinterUrlByUserId(userId);
    }

    @Override
    public List<Map<String, Object>> getSuggestTileTop1(String areaCode) {
        return suggestMapper.getSuggestTileTop1(areaCode);
    }

    @Override
    public JSONUtil getPageSuggest(String areaCode, Integer type, Integer pageNum, Integer pageSize) {
        return new JSONUtil().setData(suggestMapper.getPageSuggest(areaCode,type,(pageNum-1)*pageSize,pageSize))
                .setCountSum(suggestMapper.getPageSuggestCountNum(areaCode,type,(pageNum-1)*pageSize,pageSize));
    }

    @Override
    public JSONUtils getProductListByUserId(String userId, Integer type, Integer pageNum, Integer pageSize) {
        if (type == null)
            type = 2;//没有传入此参数时，默认请求专家产品
        if (pageNum == null)
            pageNum = 1;//默认第一页
        if (pageSize == null)
            pageSize = 10;//默认每页10条建议
        Integer currentIndex = (pageNum  - 1 )* pageSize;
        List<Map<String , Object>> suggestList = suggestMapper.getPageSuggestByUserId(userId , type , currentIndex , pageSize);
        return new JSONUtils().setData(suggestList).setCountSum(suggestMapper.getSuggestCountByUserId(userId,type));
    }

    @Override
    public JSONUtils getProductDetails(Integer productId) {
        Map<String,Object> messageDetail = suggestMapper.getSuggestDetails(productId);
        return new JSONUtils().setData(messageDetail).setCountSum(messageDetail == null ? 0 : 1);
    }

    @Override
    public JSONUtils insertSuggest(Suggest suggest) {
        int count = suggestMapper.putSuggest(suggest);
        if (count > 0)
        {
            Integer id = suggestMapper.getSuggestId();
            suggest.setId(id);
            return  new JSONUtils().setData(suggest).setCountSum(count);
        }else
        {
            return  new JSONUtils().setCountSum(count);
        }
    }
}
